import unicodedata

# 正在处理Unicode字符串，需要确保所有字符串在底层有相同的表示
def demo_1():
    s1 = 'kepler\u00f1o'
    s2 = 'keplern\u0303o'
    print('{}, {}'.format(s1, s2))
    print(s1 == s2)

    # 使用unicodedata模块先将文本标准化
    # NFC表示字符应该是整体组成
    t1 = unicodedata.normalize('NFC', s1)
    t2 = unicodedata.normalize('NFC', s2)
    print('{}, {}'.format(t1, t2))
    print(t1 == t2)
    print(ascii(t1))
    print(ascii(t2))
    # NFD表示字符应该分解为多个组合字符表示
    t3 = unicodedata.normalize('NFD', s1)
    t4 = unicodedata.normalize('NFD', s2)
    print(t3 == t4)
    print(ascii(t3))

def demo_2():
    s = '\ufb01'
    t1 = unicodedata.normalize('NFD', s)
    print(t1)
    # 支持扩展的标准化形式NFKC和NFKD,在处理某些字符的时候增加了额外的兼容特性
    t2 = unicodedata.normalize('NFKD', s)
    print(t2)
    t3 = unicodedata.normalize('NFKC', s)
    print(t3)

def demo_3():
    s1 = 'kepler\u00f1o'
    t1 = unicodedata.normalize('NFD', s1)
    print(t1)
    # combining()函数可以测试输入的参数是否为和音字符,并去掉和音字符
    result = ''.join(c for c in t1 if not unicodedata.combining(c))
    print(result)

if __name__ == '__main__':
    # demo_1()
    # demo_2()
    demo_3()